package com.net.xpay.partner.controller.partner;

import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.domain.stat.PartnerUserRealTimeStat;
import com.net.xpay.common.helper.RedisHelper;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.stat.PartnerUserRealTimeStatManager;
import com.net.xpay.common.model.PartnerUserSumModel;
import com.net.xpay.common.model.search.PartnerUserRealTimeStatSearchModel;
import com.net.xpay.common.model.search.UserSearchModel;
import com.net.xpay.common.service.PartnerUserRealTimeStatService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.constant.RedisKey;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.partner.secutiry.RequestUser;
import com.net.xpay.partner.vo.PartnerUserRealTimeStatVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author  on 20/03/2018.
 * 查询合伙人统计数据
 */
@Slf4j
@RestController
@RequestMapping("/partner/partner-user-real-time-stat")
@Api(tags = "合伙人/合伙人的商户实时统计数据", description = "合伙人 实时统计数据 zc")
public class PartnerUserRealTimeStatController {

    @Autowired
    private PartnerUserRealTimeStatManager partnerUserRealTimeStatManager;

    @Autowired
    private PartnerUserRealTimeStatService partnerUserRealTimeStatService;

    @Autowired
    private UserManager userManager;

    @Autowired
    private RedisHelper redisHelper;

    @ApiOperation("聚合实时搜索合伙人的商户数据")
    @PostMapping("/search")
    @ResponseBody
    public ResultPagedList<PartnerUserRealTimeStatVo> search(@ApiIgnore @RequestUser Partner partner, @RequestBody PartnerUserRealTimeStatSearchModel searchModel) {
        searchModel.setPartnerId(partner.getId());
        searchModel.setMinUpdateTime(LocalDateUtil.getDayStart(LocalDateTime.now()));

        List<PartnerUserRealTimeStat> records = partnerUserRealTimeStatManager.search(searchModel);
        long count = partnerUserRealTimeStatManager.searchCount(searchModel);
        List<PartnerUserRealTimeStatVo> vos = BeanCopierUtil.batchCopy(PartnerUserRealTimeStat.class, PartnerUserRealTimeStatVo.class, records);

        for (PartnerUserRealTimeStatVo vo : vos) {
            User user = userManager.getById(vo.getUserId());
            vo.setUserName(user.getName());
        }

        PartnerUserSumModel partnerUserSumModel = partnerUserRealTimeStatManager.aggSum(searchModel);
        ResultPagedList<PartnerUserRealTimeStatVo> partnerUserRealTimeStatVoResultPagedList = new ResultPagedList<>(vos, count, searchModel);
        partnerUserRealTimeStatVoResultPagedList.setExtra(partnerUserSumModel);
        return partnerUserRealTimeStatVoResultPagedList;
    }


    @ApiOperation("实时刷新")
    @PostMapping("/real-time-refresh")
    @ResponseBody
    public ResultData<Boolean> realTimeRefresh(@ApiIgnore @RequestUser Partner partner) {
        String strLimit = RedisKey.PARTNER_REAL_TIME_STAT_REFRESH + partner.getId();

        if (redisHelper.get(strLimit) == null){
            redisHelper.set(strLimit, partner.getId(),10L);
            //冻结的用户不统计 只统计正常的
            UserSearchModel searchModel = new UserSearchModel();
            searchModel.setPartnerId(partner.getId());
            List<Long> userIds = userManager.listIds(searchModel);

            if (CollectionUtils.isEmpty(userIds)) {
                return new ResultData<>(true);
            }
            for (Long userId : userIds) {
                try {
                    partnerUserRealTimeStatService.doPartnerUserRealTimeStat(userId, LocalDateTime.now());
                } catch (Exception e) {
                    log.error("realTimeRefresh error|userId={}", userId, e);
                }
            }


            return new ResultData<>(true);
        }else {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "10s后可刷新");
        }
    }

}
